package woordvoorspeller.gui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JList;
import javax.swing.ListModel;

/**
 * WordListMouseAdapter is a MouseAdapter implementation. This class
 * updates the widget when a double click if performed in the JList. 
 * It calls updateSentence method on the widget.
 * @author Brian Setz
 * @version 0.1
 */
class WordListMouseAdapter extends MouseAdapter
{
	private WordListWidget widget;
	private JList list;	
	
	/**
	 * Creates a WordListMouseAdapter .
	 * @param widget The widget that is going to use the MouseAdapter.
	 * @param list The list for retrieving the double click data. 
	 */
	public WordListMouseAdapter(WordListWidget widget, JList list)
	{
		this.widget = widget;
		this.list = list;
	}
	
	/**
	 * This event is triggered by a mouse click.
	 * @param me The MouseEvent generated by the mouse.
	 */
	public void mouseClicked(MouseEvent me)
	{
		// If the click count is not a double click then return.
		if(me.getClickCount() != 2)
			return;
			
		// Check if there are values in the list.
		int index = list.locationToIndex(me.getPoint());
		if(index == -1)
			return;
		
		// Retrieve the item on which the double click was performed.
		ListModel model = list.getModel();
		String item = (String)model.getElementAt(index);;
		list.ensureIndexIsVisible(index);
		
		// Update the widget.
		widget.updateSentence(widget.addWord(item, widget.getSentence()));	
	}
}